import 'dart:io';
import 'package:udp/udp.dart';
import 'package:path/path.dart' as path;

void sendVideo(String videoPath) async {
  // 获取桌面目录路径
  final desktopDir = await getDesktopDirectory();
  // 构建文件路径
  final filePath = path.join(desktopDir.path, 'video.mp4'); // 这里填写你的文件名
  final file = File(filePath);
  final videoData = await file.readAsBytes();
  final udpSender = await UDP.bind(Endpoint.any(port: Port(44444)));

  // 定义每个数据包的大小
  const int packetSize = 1024;

  for (int i = 0; i < videoData.length; i += packetSize) {
    final packet = videoData.sublist(i, i + packetSize > videoData.length ? videoData.length : i + packetSize);
    await udpSender.send(packet, Endpoint.broadcast(port: Port(44444)));
    await Future.delayed(Duration(milliseconds: 10)); // 控制发送速率
  }

  udpSender.close();
}

// 获取桌面目录路径的函数
Future<Directory> getDesktopDirectory() async {
  Directory directory;

  if (Platform.isWindows) {
    directory = Directory(path.join(Platform.environment['USERPROFILE']!, 'Desktop'));
  } else if (Platform.isMacOS) {
    directory = Directory(path.join(Platform.environment['HOME']!, 'Desktop'));
  } else if (Platform.isLinux) {
    directory = Directory(path.join(Platform.environment['HOME']!, 'Desktop'));
  } else {
    throw UnsupportedError('Unsupported platform');
  }

  return directory;
}
